<template>
  <div class="home">
    <div class="content">
      <div class="header">
        <el-page-header content="编辑页面" @back="goBack"></el-page-header>
      </div>
      <div class="selectMoudle">
        <div>
          <span class="text">问题</span>
          <span
            style="margin-right:20px;font-size:24px;font-weight:600"
          >{{editorData.problemData.problemname}}</span>
        </div>
        <div>
          <el-button class="sendBtn" @click="send">
            发布
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </div>
      </div>
      <div class="editor">
        <textEditor
          @EditorChange="editorChange"
          :oldcontent="editorData.html"
          :oldremoveIds="editorData.deleImgId"
        ></textEditor>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from "axios";
import textEditor from "@/components/quillEditor";
// import { textReplace } from "../../common/textReplace";
import { post_draft } from "@/api/draft";
import { get_problem } from "@/api/problem";
export default {
  name: "Editor",
  data() {
    return {
      editorData: {
        radio1: "回答",
        title: "",
        titleImageUrl: "",
        html: "",
        deleImgId: [],
        textReplace: "",
        deletTitleImageUrl: [],
        userId: this.$store.getters.userInfo._id,
        problemData: {
          problemname: ""
        }
      }
      // timer: null
    };
  },
  components: {
    textEditor
  },
  async created() {
    console.log(this.$route.query.problem_id);
    if (!this.$route.query.problem_id) {
      this.$router.push({
        path: "/help"
      });
      return;
    }
    let proData = await get_problem({
      query: {
        _id: this.$route.query.problem_id
      }
    });
    this.editorData.problemData.problemname = proData.data[0].problemname;
    this.editorData.problemData._id = proData.data[0]._id;
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    editorChange(data) {
      this.editorData.html = data.html;
      this.editorData.deleImgId = data.removeIds;
      // this.editorData.textReplace = textReplace(data.html);
      console.log(this.editorData);
      this.onChangeEdit();
    },
    async onChangeEdit() {
      let editView = await post_draft(this.editorData);
      console.log("editView", editView);
      let edit_id = editView.data._id;
      this.$router.replace({
        path: "/editAnswer",
        query: {
          _id: edit_id
        }
      });
    },
    send() {
      // if (this.editorData.articleimg == "") {
      //   return this.$alert("请添加题图", "提示", {
      //     confirmButtonText: "确定",
      //     callback: () => {}
      //   });
      // }
      // if (this.editorData.title == "") {
      //   return this.$alert("请完善文章标题", "提示", {
      //     confirmButtonText: "确定",
      //     callback: () => {}
      //   });
      // }
      if (this.editorData.html == "") {
        return this.$alert("请完善文章内容", "提示", {
          confirmButtonText: "确定",
          callback: () => {}
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  background: #fff;
}
.content {
  width: 100%;
  .header {
    margin: 0 auto;
    width: 1200px;
    margin-top: 20px;
  }
  .selectMoudle {
    margin: 0 auto;
    width: 1200px;
    margin-top: 40px;
    display: flex;
    justify-content: space-between;
    .text {
      font-size: 20px;
      font-weight: 700;
      color: #757575;
      margin-right: 20px;
    }
    .sendBtn {
      background: #1d2f70;
      border: #1d2f70;
      color: #fff;
      margin-left: 40px;
      &:hover {
        background: rgb(53, 89, 180);
      }
    }
  }
  // margin-top: -19px;
  padding: 20px;
  box-sizing: border-box;
  .editor {
    min-height: 500px;
    margin-top: 40px;
  }
}
</style>
